package com.qboot.quartz.controller;

import com.qboot.common.entity.Result;
import com.qboot.quartz.job.TestJob;
import com.qboot.quartz.service.QuartzService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Quartz 定时器
 *
 * @author Tellsea
 * @date 2023/2/14
 */
@Api(tags = "Quartz 定时器")
@RestController
@RequestMapping("/quartz")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class QuartzController {

    private final QuartzService quartzService;

    @ApiOperation("新增任务")
    @GetMapping("/addJob")
    public Result addJob() {
        Map map = new HashMap(2);
        map.put("id", 1L);
        return quartzService.addJob(TestJob.class, "job", "test", "0/5 * * * * ?", map);
    }

    @ApiOperation("更新任务")
    @GetMapping("/updateJob")
    public Result updateJob() {
        return quartzService.updateJob("job", "test", "0/10 * * * * ?");
    }

    @ApiOperation("删除任务")
    @GetMapping("/deleteJob")
    public Result deleteJob() {
        return quartzService.deleteJob("job", "test");
    }

    @ApiOperation("暂停任务")
    @GetMapping("/pauseJob")
    public Result pauseJob() {
        return quartzService.pauseJob("job", "test");
    }

    @ApiOperation("恢复任务")
    @GetMapping("/resumeJob")
    public Result resumeJob() {
        return quartzService.resumeJob("job", "test");
    }

    @ApiOperation("立即执行任务")
    @GetMapping("/runAJobNow")
    public Result runAJobNow() {
        return quartzService.runAJobNow("job", "test");
    }

    @ApiOperation("所有任务列表")
    @GetMapping("/queryAllJob")
    public Result queryAllJob() {
        return quartzService.queryAllJob();
    }

    @ApiOperation("所有任务列表（运行中）")
    @GetMapping("/queryRunJob")
    public Result queryRunJob() {
        return quartzService.queryRunJob();
    }
}
